<?php

class Usermodel extends CI_Model{
	
	var $conn;
	
	function __construct()
    {
        // Call the Model constructor
        parent::__construct();
		$user = "automatsystem";
		$pass = "freemachineskillhumans";
		error_reporting(0);
		$this->conn = mysql_connect("localhost:3306",$user,$pass);
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3307",$user,$pass);	
		}
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3308",$user,$pass);	
		}
		if(!$this->conn){
			$this->conn = mysql_connect("localhost:3309",$user,$pass);
		}
		error_reporting(E_ALL);
		if(!$this->conn){
			echo '*ERROR* Usermodel: Can\'t connect to MySQL server.';
		}
		else{
			mysql_select_db("automatsystem");
		}
    }

	function testConnection(){
		if($this->conn){return true;}
		return false;
	}
	
	/*
		Returns columns : UserId,Login,CreationDate,Email,Name,Surname,Telephone,IsActivated,DeleteRequest.
		Example:
			$query = $this->Usermodel->getAllUsers();
			echo $query[0]['UserId'];
	*/
	function getAllUsers(){
		$queryRes = mysql_query("SELECT UserId,Login,CreationDate,Email,
                    Name,Surname,TelNumber,IsActivated,DeleteRequest FROM User;",$this->conn);
		$retArr = array();
		while($row = mysql_fetch_assoc($queryRes)){
			$retArr[] = $row;
		}
		return $retArr;
	}
	
	function addUser($login, $pass, $email){
		if($this->loginExists($login)){
			return false;
		}
		$res = mysql_query("INSERT INTO User(Login,Password,Email,CreationDate,IsActivated,DeleteRequest) VALUES('".$login."','".$pass."','".$email."',NOW(),0,0);",$this->conn);
		if(!$res){echo '<p>Error adding user. </p>';}
		return $res;
	}
		
	function deleteUser($userId){
		$res = mysql_query("DELETE FROM User WHERE UserId=".$userId.";");
		if(!$res){echo '<p>Error deleting user. </p>';}
		return $res;
	}
	
	function loginPassValid($login,$pass){
		$res = mysql_query("SELECT * FROM User WHERE Login='".$login."' AND Password='".$pass."';");
		if(mysql_num_rows($res)){ return true; }
		return false;
	}
	
	function changePass($userId, $newPass){
		$res = mysql_query("UPDATE User SET Password='".$newPass."' WHERE UserId=".$userId.";");
		if(!$res){echo '<p>Error updating password. </p>';}
		return $res;
	}
	
	function getUserIdByLogin($login){
		$queryRes = mysql_query("SELECT UserId FROM User WHERE Login='".$login."';",$this->conn);
		$firstRow = mysql_fetch_assoc($queryRes);
		return $firstRow['UserId'];
	}
        
    function loginExists($login){
        $res = mysql_query("SELECT * FROM User WHERE Login='".$login."';");
        if(mysql_num_rows($res)){ return true; }
        return false;
    }
	
    function setUserData($userId, $name, $surname, $email, $telephone){
        $res = mysql_query("UPDATE User SET Email='".$email.
            "', Name='".$name.
			"', Surname='".$surname.
            "', TelNumber='".$telephone.
            "' WHERE UserId=".$userId.";");
        if(!$res){
            echo "<p>Error setting user's data. </p>";
            echo mysql_error();
        }
        return $res;
    }
		
	function getUserData($login) {
		$userDataQuery = mysql_query("SELECT * FROM User WHERE Login='".$login."';");
		$userData = mysql_fetch_assoc($userDataQuery);
		return $userData;
	}
        
    function setDeleteRequest($userId, $boolVal){
        $res = mysql_query("UPDATE User SET DeleteRequest=".($boolVal?'1':'0').
			" WHERE UserId=".$userId.";");
        if(!$res){
            echo "<p>Error setting delete request. </p>";
            echo mysql_error();
        }
    }
        
    function setIsActivated($userId, $boolVal){
        $res = mysql_query("UPDATE User SET IsActivated=".($boolVal?'1':'0').
            " WHERE UserId=".$userId.";");
        if(!$res){
            echo "<p>Error setting IsActivated. </p>";
            echo mysql_error();
        }
    }
		
	function getUnconfirmedUsers(){
		$queryRes = mysql_query("SELECT * FROM User WHERE IsActivated=0 AND DeleteRequest=0;",$this->conn);
		$users = array();
		while($row = mysql_fetch_assoc($queryRes)){
			$users[] = $row;
		}
		return $users;
	}
		
	function getConfirmedUsers(){
		$queryRes = mysql_query("SELECT * FROM User WHERE IsActivated=1;",$this->conn);
		$users = array();
		while($row = mysql_fetch_assoc($queryRes)){
			$users[] = $row;
		}
		return $users;
	}
		
	function getDeactivatedUsers(){
		$queryRes = mysql_query("SELECT * FROM User WHERE DeleteRequest=1;",$this->conn);
		$users = array();
		while($row = mysql_fetch_assoc($queryRes)){
			$users[] = $row;
		}
		return $users;
	}
	
	function isUserConfirmed($userId)
	{
		$res = mysql_query("SELECT * FROM User WHERE UserId=".$userId." AND IsActivated=1;");
		if(mysql_num_rows($res)){ return true; }
		return false;
	}
}